<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!-- <el-form-item label="达人信息表ID" prop="sampleTalentInfoId">
        <el-input
          v-model="queryParams.sampleTalentInfoId"
          placeholder="请输入达人信息表ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="支付方式" prop="payMethod">
        <el-input v-model="queryParams.payMethod" placeholder="请输入支付方式" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="支付账号" prop="payAccount">
        <el-input v-model="queryParams.payAccount" placeholder="请输入支付账号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="姓名" prop="payName">
        <el-input v-model="queryParams.payName" placeholder="请输入姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="逻辑删除;0=未删除；1=已删除" prop="isDelete">
        <el-input
          v-model="queryParams.isDelete"
          placeholder="请输入逻辑删除;0=未删除；1=已删除"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['product:pay:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['product:pay:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['product:pay:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['product:pay:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="payList" @selection-change="handleSelectionChange" @row-dblclick="t">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="支付ID" align="center" prop="id" />
      <el-table-column label="达人信息表ID" align="center" prop="sampleTalentInfoId" /> -->
      <el-table-column label="平台ID" align="center" prop="platformId" />
      <el-table-column label="昵称" align="center" prop="xhsNickname" />
      <el-table-column label="支付方式" align="center" prop="payMethod" />
      <el-table-column label="支付账号" align="center" prop="payAccount" />
      <el-table-column label="收款码" align="center" prop="payImage" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.payImage" :width="50" :height="50" />
        </template>

      </el-table-column>
      <el-table-column label="姓名" align="center" prop="payName" />
      <el-table-column label="备注" align="center" prop="remark" />
      <!-- <el-table-column label="逻辑删除;0=未删除；1=已删除" align="center" prop="isDelete" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['product:pay:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['product:pay:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改 达人信息-支付信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!-- <el-form-item label="达人信息表ID" prop="sampleTalentInfoId">
          <el-input v-model="form.sampleTalentInfoId" placeholder="请输入达人信息表ID" />
        </el-form-item> -->
        <el-form-item label="支付方式" prop="payMethod">
          <el-input v-model="form.payMethod" placeholder="请输入支付方式" />
        </el-form-item>
        <el-form-item label="支付账号" prop="payAccount">
          <el-input v-model="form.payAccount" placeholder="请输入支付账号" />
        </el-form-item>
        <el-form-item label="收款码" prop="payImage">
          <!-- <image-upload v-model="form.payImage" /> -->
          <el-upload ref="uploadPay" :action="imgUpload.url" :headers="imgUpload.headers" list-type="picture-card"
            :on-change='changePay' :file-list="filePay" multiple :on-success="uploadPay" :on-remove="RemovePay">
            <i class="el-icon-plus"></i>
          </el-upload>
          <div id="preview" @paste="handlePay">
            <span><i class="el-icon-s-opportunity" style="color:#FB894C"></i>点击此处 将图片按Ctrl+V 粘贴至此处</span>
          </div>
        </el-form-item>
        <el-form-item label="姓名" prop="payName">
          <el-input v-model="form.payName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <!-- <el-form-item label="逻辑删除;0=未删除；1=已删除" prop="isDelete">
          <el-input v-model="form.isDelete" placeholder="请输入逻辑删除;0=未删除；1=已删除" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPay, getPay, delPay, addPay, updatePay } from "@/api/sample/pay";
//--------导入token
import { getToken } from "@/utils/auth";
export default {
  name: "Pay",
  data() {
    return {
      payImage: '',
      filePay: [],
      pjtUrl: process.env.VUE_APP_BASE_API,
      // 图片数量限制
      limit: 2,
      imgUpload: {
        // 设置上传的请求头部
        headers: {
          Authorization: "Bearer " + getToken()
        },
        // 图片上传的方法地址:
        url: process.env.VUE_APP_BASE_API + "/common/upload",
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      //  达人信息-支付信息表格数据
      payList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sampleTalentInfoId: null,
        payMethod: null,
        payAccount: null,
        payImage: null,
        payName: null,
        isDelete: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        // sampleTalentInfoId: [
        //   { required: true, message: "达人信息表ID不能为空", trigger: "blur" }
        // ],
        // payMethod: [
        //   { required: true, message: "支付方式不能为空", trigger: "blur" }
        // ],
        // payAccount: [
        //   { required: true, message: "支付账号不能为空", trigger: "blur" }
        // ],
        // payImage: [
        //   { required: true, message: "支付账号不能为空", trigger: "blur" }
        // ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //点击某一行
    t(row, column, cell, event) {
      console.log(row, column, cell, event);
      this.handleUpdate(row)
    },
    /** 查询 达人信息-支付信息列表 */
    getList() {
      let queryParam = null

      queryParam = this.$route.query.id
      this.loading = true;
      listPay({ sampleTalentInfoId: queryParam, payMethod: this.queryParams.payMethod, payAccount: this.queryParams.payAccount, payName: this.queryParams.payName }).then(response => {
        this.payList = response.rows;
        console.log(JSON.stringify(this.payList));
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        sampleTalentInfoId: null,
        payMethod: null,
        payAccount: null,
        payImage: null,
        payName: null,
        remark: null,
        isDelete: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      if (!this.$route.query.id) {
        this.$modal.msgError("禁止添加,请从达人信息菜单-支付配置进入");
        return
      }
      this.reset();
      this.open = true;
      this.title = "添加 达人信息-支付信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {

      this.reset();
      const id = row.id || this.ids
      getPay(id).then(response => {
        this.filePay = response.data.payImage ? [
          { url: process.env.VUE_APP_BASE_API + response.data.payImage }] : []//payImage图片加的是这一行
        this.form = response.data;
        this.payImage = response.data.payImage
        this.open = true;
        this.title = "修改 达人信息-支付信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.form.payImage = this.payImage
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePay(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.sampleTalentInfoId = this.$route.query.id
            addPay(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除 达人信息-支付信息编号为"' + ids + '"的数据项？').then(function () {
        return delPay(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('product/pay/export', {
        ...this.queryParams
      }, `pay_${new Date().getTime()}.xlsx`)
    },
    //handlePay图片
    handlePay(event) {
      const items = (event.clipboardData || window.clipboardData).items;
      let file = null;
      if (!items || items.length === 0) {
        this.$message.error("当前浏览器不支持本地");
        return;
      }
      // 搜索剪切板items
      for (let i = 0; i < items.length; i++) {
        if (items[i].type.indexOf("image") !== -1) {
          file = items[i].getAsFile();
          break;
        }
      }
      if (!file) {
        this.$message.error("粘贴内容非图片");
        return;
      }
      if (this.filePay.length >= this.limit) {
        this.$message.error(`上传文件数量不能超过 ${this.limit} 个!`); // 图片数量超出
        return
      }
      this.$refs.uploadPay.handleStart(file); // 将粘贴过来的图片加入预上传队列
      this.$refs.uploadPay.submit(); // 提交图片上传队列
    },
    //上传
    changePay(file, fileList) {
      // this.fileList = fileList
      console.log('成功', file, fileList)
      console.log(JSON.stringify(file));
      console.log(JSON.stringify(fileList));
      if (fileList.length > 1) {
        this.filePay = fileList.slice(-1);

      }
    },
    RemovePay() {
      this.payImage = ''
    },

    uploadPay(res, file, fileList) {
      console.log(JSON.stringify(file.response.fileName));
      this.payImage = file.response.fileName

    },
  }
};
</script>
